package com.das.mechanic_base.adapter.record;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.record.ReceiveListBean;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3RecordRelateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ReceiveListBean> mList;

    private Context mContext;

    private List<Boolean> sList;

    /**
     * 空数据时，显示空布局类型
     */
    private final int EMPTY_VIEW = 1;
    /**
     * 控制空布局的显隐
     */
    private int mEmptyType = 0;

    public X3RecordRelateAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        sList = new ArrayList<>();
    }

    public void changeData(List<ReceiveListBean> mList) {
        this.mList.clear();
        this.mList = mList;
        if (X3StringUtils.isListEmpty(mList)) {
            mEmptyType = 1;
        }else {
            mEmptyType = 0;
            for (int i = 0; i < mList.size(); i++) {
                sList.add(false);
            }
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return  new RecordRelateHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_record_relate_item,parent,false));
            case 1:
                return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_record_item, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == 0) {
            RecordRelateHolder holder1 = (RecordRelateHolder) holder;

            holder1.tv_key.setText(mList.get(position).receiveBaseKey);
            holder1.tv_car_num.setText(mList.get(position).carNum + "");

            holder1.tv_time.setText(X3DateUtils.LongDateWithTime(mList.get(position).mgtCreate));

            if (sList.get(position)) {
                holder1.itemView.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_shape_idea_select_item));
                holder1.tv_key.setTextColor(Color.parseColor("#0077ff"));
                holder1.tv_car_num.setTextColor(Color.parseColor("#0077ff"));
                holder1.tv_time.setTextColor(Color.parseColor("#0077ff"));
            }else {
                holder1.itemView.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_shape_idea_unselect_item));
                holder1.tv_key.setTextColor(Color.parseColor("#333333"));
                holder1.tv_car_num.setTextColor(Color.parseColor("#333333"));
                holder1.tv_time.setTextColor(Color.parseColor("#666666"));
            }

            holder1.itemView.setOnClickListener(v -> {
                for (int i = 0; i < sList.size(); i++) {
                    if (i == position) {
                        Boolean aBoolean = sList.get(position);
                        sList.remove(i);
                        if (aBoolean) {
                            sList.add(i,false);
                        }else {
                            sList.add(i,true);
                        }
                    }else {
                        sList.remove(i);
                        sList.add(i,false);
                    }
                }

                long receiveId = 0;
                String receiveKey = "";
                if (sList.get(position)) {
                    receiveId = mList.get(position).receiveBaseId;
                    receiveKey = mList.get(position).receiveBaseKey;
                }

                notifyDataSetChanged();
                if (iOnClickSelectReceive != null) {
                    iOnClickSelectReceive.iOnClickSelectReceive(receiveId,receiveKey);
                }
            });
        }else {
            EmptyHolder holder1 = (EmptyHolder) holder;
            holder1.tv_empty.setText(mContext.getString(R.string.x3_now_select_no_data));
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (mEmptyType == EMPTY_VIEW) {
            //空布局的类型
            return 1;
        }
        return 0;
    }

    IOnClickSelectReceive iOnClickSelectReceive;

    public void setiOnClickSelectReceive(IOnClickSelectReceive iOnClickSelectReceive) {
        this.iOnClickSelectReceive = iOnClickSelectReceive;
    }

    public interface IOnClickSelectReceive {
        void iOnClickSelectReceive(long id,String receiveBaseKey);
    }

    @Override
    public int getItemCount() {
        if (X3StringUtils.isListEmpty(mList)) {
            return mEmptyType;
        }
        return mList.size();
    }

    class RecordRelateHolder extends RecyclerView.ViewHolder {
        TextView tv_key;
        TextView tv_car_num;
        TextView tv_time;
        public RecordRelateHolder(@NonNull View itemView) {
            super(itemView);
            tv_key = itemView.findViewById(R.id.tv_key);
            tv_car_num = itemView.findViewById(R.id.tv_car_num);
            tv_time = itemView.findViewById(R.id.tv_time);
        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;
        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
        }
    }
}
